#include "main.h"
/*
    声卡信息：
        采样率：    48000
        采样格式：  S16
        声道        双声道
        采样数      1024
        节点信息：    "default"//默认声卡
    AENC信息：
        编码输入：  S16-48000-2-PCM
        编码输出：  MP2
        比特率 :   64Kbps
        码率格式：  CBR(固定码率)
        码率速度：  1920*1080
        GOP组数：  30



*/

void Aenc_callback_funciton(MEDIA_BUFFER mb)
{
    uint8_t * data = RK_MPI_MB_GetPtr(mb); 
    uint32_t size = RK_MPI_MB_GetSize(mb);
    //printf("ANEC数据！\r\n");
    if(h_info.auido_net_status == true)
    {
        HELMET_MSG sendauido_msg={
            .xyd="xyd",
        };
        sendauido_msg.command = XYD_MSG_AUIDO;
        sendauido_msg.data_length = size;
        sendauido_msg.data_ptr = data;
        sendauido_msg.device_id = h_info.device_id;
        sendauido_msg.sum = sum_value(sendauido_msg);
        mysock_write(h_info.sockfd,&sendauido_msg,sizeof(sendauido_msg));
        mysock_write(h_info.sockfd,sendauido_msg.data_ptr,sendauido_msg.data_length);
    }
    RK_MPI_MB_ReleaseBuffer(mb);
    size = size;
    data= data;
}

void Init_Audio(void)
{
    int ret = 0;
//1：RKMEdoia的初始化 
    //RK_MPI_SYS_Init();
//2：创建 AI
   
	AI_CHN_ATTR_S ai_attr;
	ai_attr.pcAudioNode = ai_node;
	ai_attr.enSampleFormat = RK_SAMPLE_FMT_S16;
	ai_attr.u32NbSamples = 1152;
	ai_attr.u32SampleRate = 48000;
	ai_attr.u32Channels = 2;
	ai_attr.enAiLayout = AI_LAYOUT_NORMAL;
	ret = RK_MPI_AI_SetChnAttr(0, &ai_attr);
	ret |= RK_MPI_AI_EnableChn(0);
	if (ret) {
		printf("Create AI[0] failed! ret=%d\n", ret);
		return ;
	}


//3:创建 AENC通道
	// Create AENC
	AENC_CHN_ATTR_S aenc_attr;
	aenc_attr.enCodecType = RK_CODEC_TYPE_MP2;
	aenc_attr.u32Bitrate = 64000;
	aenc_attr.u32Quality = 1;
	aenc_attr.stAencMP3.u32Channels = 2;
	aenc_attr.stAencMP3.u32SampleRate = 48000;
	ret = RK_MPI_AENC_CreateChn(0, &aenc_attr);
	if (ret) {
		printf("Create AENC[0] failed! ret=%d\n", ret);
		return ;
	}
    aenc_attr.enCodecType = RK_CODEC_TYPE_G711A;
	aenc_attr.u32Bitrate = 64000;
	aenc_attr.u32Quality = 1;
	aenc_attr.stAencG711A.u32Channels = 2;
	aenc_attr.stAencG711A.u32SampleRate = 48000;
    aenc_attr.stAencG711A.u32NbSample = 1152;//MP2固定大小
    RK_MPI_AENC_CreateChn(1, &aenc_attr);
//4:绑定 AI->AENC
    MPP_CHN_S src,dst;
    src.enModId=RK_ID_AI;
    src.s32ChnId=0;
    src.s32DevId=0;

    dst.enModId = RK_ID_AENC;
    dst.s32ChnId=0;
    dst.s32DevId = 0;
    RK_MPI_SYS_Bind(&src,&dst);    
    
//5:注册AENC回调函数
    dst.enModId = RK_ID_AENC;
    dst.s32ChnId=1;
    dst.s32DevId = 0;
    RK_MPI_SYS_RegisterOutCb(&dst,Aenc_callback_funciton);
    RK_MPI_SYS_Bind(&src,&dst);

//6:创建 ADEC解码通道 
    
	ADEC_CHN_ATTR_S stAdecAttr = {0};
	stAdecAttr.enCodecType = RK_CODEC_TYPE_G711A;
    stAdecAttr.stAdecG711A.u32Channels = 2;
    stAdecAttr.stAdecG711A.u32SampleRate = 48000;
    RK_MPI_ADEC_CreateChn(0, &stAdecAttr);
//7：创建 AO 输出通道
    AO_CHN_ATTR_S stAoAttr;
	stAoAttr.u32Channels = 2;
	stAoAttr.u32SampleRate = 48000;
	stAoAttr.u32NbSamples = 1024;
	stAoAttr.pcAudioNode = ao_node;
	stAoAttr.enSampleFormat = RK_SAMPLE_FMT_S16;
    ret = RK_MPI_AO_SetChnAttr(0, &stAoAttr);
	ret |= RK_MPI_AO_EnableChn(0);
	if (ret) {
		printf("ERROR: Create AO[0] failed! ret=%d\n", ret);
		return ;
	}
    src.enModId=RK_ID_ADEC;
    src.s32ChnId=0;
    src.s32DevId=0;

    dst.enModId = RK_ID_AO;
    dst.s32ChnId=0;
    dst.s32DevId = 0;
    ret = RK_MPI_SYS_Bind(&src, &dst);
	if (ret) {
		printf("ERROR: Bind ADEC[0] to AO[0] failed! ret=%d\n", ret);
		return ;
	}

}


void Recv_Audio_data(HELMET_MSG msg)
{   
    msg = msg;
	MEDIA_BUFFER mb = NULL;
	MB_AUDIO_INFO_S stSampleInfo = {2, 48000,
                                  1024,
                                  RK_SAMPLE_FMT_S16};
    mb = RK_MPI_MB_CreateAudioBufferExt(&stSampleInfo, RK_FALSE, 0);
    int ret = mysock_read(h_info.sockfd,RK_MPI_MB_GetPtr(mb),RK_MPI_MB_GetSize(mb));
    
    printf("ret is %d\r\n",ret);
    // if(!sum_test(msg))
    // {
    //     printf("sum_test(msg) is %d\r\n",sum_test(msg));
    //     RK_MPI_MB_ReleaseBuffer(mb);
    //     mb = NULL;
    //     printf("接受音频消息有误！将丢弃本次消息！\r\n");
    //     return ;
    // }
    RK_MPI_MB_SetSize(mb, ret);	//设置 mb包大小
    RK_MPI_SYS_SendMediaBuffer(RK_ID_ADEC, 0,mb);
    RK_MPI_MB_ReleaseBuffer(mb);
    mb = NULL;

}